package com.ultrapower.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ultrapower.commons.result.PageInfo;
import com.ultrapower.commons.result.Result;
import com.ultrapower.commons.utils.McAesencrypt;
import com.ultrapower.mapper.ResultMapper;
import com.ultrapower.service.IResultService;

/**
*
* Result 表数据服务层接口实现类
*
*/
@Service
public class ResultServiceImpl extends ServiceImpl<ResultMapper, Result> implements IResultService{

	@Autowired
    private ResultMapper resultMapper;
	
	@Override
	public void selectDataGrid(PageInfo pageInfo) {
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
        page.setOrderByField(pageInfo.getSort());
        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
        List<Map<String, Object>> list = resultMapper.selectResultPage(page, pageInfo.getCondition());
        //此处对imei、身份证、号码进行解密  
        List<String> imeis = new ArrayList<String>();
        List<String> cids = new ArrayList<String>();
        List<String> phones = new ArrayList<String>();
        for(int i=0;i<list.size();i++){
        	imeis.add(list.get(i).get("imei").toString());
        	cids.add(list.get(i).get("idNumber").toString());
        	phones.add(list.get(i).get("telephone").toString());
        }
        //解密  
        try {
			imeis = McAesencrypt.MCAesdecrypt(imeis, "imei");
			cids = McAesencrypt.MCAesdecrypt(cids, "certificateNo");
		    phones = McAesencrypt.MCAesdecrypt(phones, "phoneNo");
		} catch (Exception e) {
			e.printStackTrace();
		}
        
        //将解密后的信息放到list里面
        for(int i=0;i<list.size();i++){
        	list.get(i).put("imei", imeis.get(i));
        	list.get(i).put("idNumber",cids.get(i));
        	list.get(i).put("telephone",phones.get(i));
        }
       
        pageInfo.setData(list);
        pageInfo.setCount(page.getTotal());
	}

	@Override
	public List<String> getSendMsgData() {
		List<String> list = resultMapper.selectSendMsgData();
		return list;
	}

	@Override
	public void updateState(String phone) {
		resultMapper.updateState(phone);
	}

	@Override
	public void updateReplyMsg(Map<String, Object> condition) {
		resultMapper.updateReplyMsg(condition);
	}
}
